#include <iostream>
#include <string>
#define MAX 1000
using namespace std;

//设计联系人的结构体
struct Person
{
    //姓名
    string name;
    //性别  1男  2女
    int sex;
    //年龄
    int age;
    //电话
    string phone;
    //住址
    string address;
};

//设计通讯录的结构体
struct Addressbook
{
    //通讯录中，保存的联系人的数组
    struct Person personArray[MAX];
    
    //通讯录中当前记录联系人的个数
    int size;
};

//封装函数——显示菜单界面
void showMenu()
{
    cout << "***************" << endl;
    cout << "*1、添加联系人*" << endl;
    cout << "*2、显示联系人*" << endl;
    cout << "*3、删除联系人*" << endl;
    cout << "*4、查找联系人*" << endl;
    cout << "*5、修改联系人*" << endl;
    cout << "*6、清空联系人*" << endl;
    cout << "*0、退出通讯录*" << endl;
    cout << "***************" << endl;
}

//1、添加联系人
void addPerson(Addressbook* abs)
{
    //判断通讯录是否已满
    if (abs->size == MAX)
    {
        cout << "通讯录已满，无法添加！" << endl;
        return;
    }
    //添加具体联系人
    //姓名
    string name;
    cout << "请输入姓名：" << endl;
    cin >> name;
    abs->personArray[abs->size].name = name;
    
    //性别
    while (true)
    {
        cout << "请输入性别：" << endl;
        cout << "1--男" << endl;
        cout << "2--女" << endl;
        int sex = 0;
        cin >> sex;
        if (sex == 1 || sex == 2)
        {
            abs->personArray[abs->size].sex = sex;
            break;
        }
        else {
            cout << "输入内容无效，请重新输入：" << endl;
        }
    }
    
    //年龄
    cout << "请输入年龄：" << endl;
    int age = 0;
    cin >> age;
    abs->personArray[abs->size].age = age;

    //电话
    cout << "请输入联系电话：" << endl;
    string phone;
    cin >> phone;
    abs->personArray[abs->size].phone = phone;

    //住址
    cout << "请输入家庭住址：" << endl;
    string address;
    cin >> address;
    abs->personArray[abs->size].address = address;

    //更新size
    abs->size++;

    system("clear"); //功能：清屏
    cout << "恭喜您添加联系人成功！" << endl;
}

//2、显示联系人
void showPerson(Addressbook* abs)
{
    //判断通讯录中，人数是否为0
    if (abs->size == 0)
    {
        cout << "当前手机内无联系人！" << endl;
    }
    else
    {
        for (int i = 0;i < abs->size;i++)
        {
            cout << "姓名：" << abs->personArray[i].name << "\t"
                << "  性别：" << (abs->personArray[i].sex == 1 ? "男" : "女") << "\t"
                << "  年龄：" << abs->personArray[i].age << "\t"
                << "  电话：" << abs->personArray[i].phone << "\t"
                << "  家庭住址：" << abs->personArray[i].address << endl;
        }
    }
}

//检测联系人是否存在，存在则返回位置，不存在返回-1
int isExist(Addressbook *abs,string name)
{
    for (int i = 0;i < abs->size;i++)
    {
        //找到用户输入的姓名
        if (abs->personArray[i].name == name)
        {
            return i;
        }
    }
    //未找到，返回-1
    return -1;
}

//3、删除指定的联系人
void deletePerson(Addressbook * abs)
{
    cout << "请输入您要删除的联系人：" << endl;

    string name;
    cin >> name;
    //ret == -1, 未找到
    //ret != -1, 查到了
    int ret = isExist(abs, name);

    if (ret != -1)
    {
        //查到此人,删除
        for (int i = ret;i < abs->size;i++)
        {
            abs->personArray[i] = abs->personArray[i + 1];
        }
        abs->size--;
        cout << "删除成功！" << endl;
    }
    else
    {
        //未查到此人
        cout << "查无此人！" << endl;
    }
}

//4、查找指定的联系人信息
void findPerson(Addressbook* abs)
{
    cout << "请输入您要查找的联系人姓名：" << endl;

    string name;
    cin >> name;

    //判断联系人是否存在
    int ret = isExist(abs, name);
    if (ret != -1)
    {
        //找到该联系人
        cout << "姓名：" << abs->personArray[ret].name << "\t"
            << "  性别：" << (abs->personArray[ret].sex == 1 ? "男" : "女") << "\t"
            << "  年龄：" << abs->personArray[ret].age << "\t"
            << "  电话：" << abs->personArray[ret].phone << "\t"
            << "  家庭住址：" << abs->personArray[ret].address << endl;
    }
    else
    {
        //未找到联系人
        cout << "查无此人！" << endl;
    }
}

//5、修改联系人
void modifyPerson(Addressbook* abs)
{
    cout << "请输入您要修改的联系人：" << endl;
    string name;
    cin >> name;

    int ret = isExist(abs, name);
    if(ret != -1)
    {
        //找到联系人
        //修改姓名
        string name;
        cout << "请输入修改后的姓名：" << endl;
        cin >> name;
        abs->personArray[ret].name = name;

        //修改性别
        while (1)
        {
            cout << "请输入修改后的性别：" << endl;
            cout << "1--男" << endl;
            cout << "2--女" << endl;
            int sex = 0;
            cin >> sex;
            if (sex == 1 || sex == 2)
            {
                abs->personArray[ret].sex = sex;
                break;
            }
            else
            {
                cout << "输入的性别无效，请重新输入！" << endl;
            }
        }

        //修改年龄
        cout << "请输入修改后的年龄：" << endl;
        int age = 0;
        cin >> age;
        abs->personArray[ret].age = age;
        
        //修改电话
        cout << "请输入修改后的联系电话：" << endl;
        string phone;
        cin >> phone;
        abs->personArray[ret].phone = phone;

        //修改家庭住址
        cout << "请输入修改后的家庭住址：" << endl;
        string address;
        cin >> address;
        abs->personArray[ret].address = address;

        cout << "修改成功！" << endl;
    }
    else
    {
        //未找到联系人
        cout << "查无此人！" << endl;
    }
}

//6、清空联系人
void cleanPerson(Addressbook* abs)
{
    abs->size = 0;
    cout << "已成功清空通讯录！" << endl;
}

int main()
{
    //创建通讯录结构体变量
    Addressbook abs;
    //初始化通讯录中，当前人员的个数
    abs.size = 0;
    
    int select = 0; //创造用户选择输入的变量

    while (true)
    {
        //调用菜单
        showMenu();

        cin >> select;

        switch (select)
        {
        case 1://添加联系人
            addPerson(&abs);
            break;
        case 2://显示联系人
            showPerson(&abs);
            break;
        case 3://删除联系人
            deletePerson(&abs);
            break;
        case 4://查找联系人
            findPerson(&abs);
            break;
        case 5://修改联系人
            modifyPerson(&abs);
            break;
        case 6://清空联系人
            cleanPerson(&abs);
            break;
        case 0://退出
            cout << "已成功退出通讯录系统，欢迎下次使用！" << endl;
            return 0;
            break;
        }
    }
}

